package a66weding.com.jiehuntong.activity.order;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.order.adapter.RefundAdapter;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MyOrderBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.CommonAdapter;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;

/**
 * Created by admin on 2016/9/1.
 * 退款;
 */
public class RefundActivity extends Activity implements OnNetResultListener, View.OnClickListener {
    private LinearLayout mBackLay;
    private TextView mBackTv;
    private TextView mHeadeTitle;
    private TextView mRightFunc;

    private RefundAdapter adapter;
    private ListView refund_lv;
    private TextView mOrdernumTv;
    private TextView mReasonTv;
    private TextView mProcessTv;

    MyOrderBean.DataBean.MyOrderDetailBean myOrderDetailBean;
    private int i_returnId;
    private List<MyOrderBean.DataBean.MyOrderDetailBean.OrderOperateInfosBean> orderOperateInfoList;
    private CommonAdapter mAdapter;
    private String ordersNumber;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund);
        Intent intent = getIntent();
        if(intent!=null) {
            myOrderDetailBean = ( MyOrderBean.DataBean.MyOrderDetailBean) intent.getSerializableExtra("OrderDetailsAdapter");
            if (myOrderDetailBean.getOrderReturn() != null){
                i_returnId = myOrderDetailBean.getOrderReturn().getI_Id();
            }
            orderOperateInfoList =myOrderDetailBean.getOrderOperateInfos();
            ordersNumber = intent.getStringExtra("ordersNumber");
        }

        initView();
        initData();
    }


    private void initView(){

        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mBackTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mHeadeTitle = (TextView) findViewById(R.id.tv_header_title);
        mRightFunc = (TextView) findViewById(R.id.tv_header_function);
        refund_lv = (ListView) findViewById(R.id.refund_lv);
        mOrdernumTv = (TextView) findViewById(R.id.tv_orderNum);
        mReasonTv = (TextView) findViewById(R.id.tv_cancel_reason);
        mProcessTv = (TextView) findViewById(R.id.tv_cancel_process);

        mBackTv.setText("返回");
        mRightFunc.setVisibility(View.GONE);

        if(myOrderDetailBean!=null) {
            mOrdernumTv.setText(ordersNumber);
            if (myOrderDetailBean.getOrderReturn() != null) {
                String nvc_content = myOrderDetailBean.getOrderReturn().getNvc_Content();
                mReasonTv.setText(nvc_content);
            }
            if (orderOperateInfoList.size() > 0) {
                String remark = myOrderDetailBean.getOrderOperateInfos().get(0).getRemark();
                mProcessTv.setText(remark);
            }
            for (int i = 0;i < orderOperateInfoList.size(); i++) {
                String type = myOrderDetailBean.getOrderOperateInfos().get(i).getNvc_Type();
                if ("取消".equals(type)){
                    Toast.makeText(RefundActivity.this,"您的申请被取消",Toast.LENGTH_SHORT).show();
                }
            }
        }

        mBackLay.setOnClickListener(this);

    }

    private void initData(){
        mHeadeTitle.setText("取消/退款进度");

        setAdapter();


    }

    private void getNetData(int returnId){
        String url = RequestCode.get_OrderReturnJinDu;

        url += "&returnID="+returnId;

        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.GETREQUEST, url, this, CommonResultBean.class, RequestCode.get_OrderReturnJinDu);
        HttpHandleUtil.executeCommonGet(this,httpRequestBean);
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();

    }

    @Override
    public void onError(ErrorBean errorBean) {

    }



    private void setAdapter(){
        if(orderOperateInfoList!=null) {
            adapter = new RefundAdapter(this,orderOperateInfoList);
            refund_lv.setAdapter(adapter);
            setListViewHeightBasedOnChildren(refund_lv);

        }

    }



    @Override
    protected void onResume() {
        super.onResume();
    }

    public void back(View view){
        finish();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
                case R.id.llay_header_back:
                    finish();

                break;

        }


    }


    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}
